package com.module.my.view

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.module.app.base.BaseListActivity
import com.module.app.bean.ListViewBean
import com.module.app.bean.PayBean
import com.module.app.pop.PayPop
import com.module.app.utils.UserUtils
import com.module.base.base.adapter.BaseAdapter
import com.module.base.base.adapter.BaseNewAdapter
import com.module.base.utils.CollectionUtil
import com.module.base.utils.RecycleViewUtil
import com.module.base.utils.ToastUtils
import com.module.frame.ext.observe
import com.module.my.R
import com.module.my.adapter.VipAdapter
import com.module.my.adapter.holder.vip.VipPriceHolder
import com.module.my.bean.VipConfigBean
import com.module.my.databinding.MyActivityVipBinding
import com.module.my.model.VipViewModel
import com.module.third.bean.IThirdType
import com.module.third.ext.payLiveSharedFlow
import java.math.BigDecimal

class VipActivity : BaseListActivity<VipViewModel, MyActivityVipBinding, VipAdapter>() {

    //设置顶部
    private val headView: View by lazy {
        LayoutInflater.from(getContext()).inflate(R.layout.my_activity_vip_head, null)
    }
    private var recyclerviewPrice: RecyclerView? = null
    private var selectPosition = 0


    override fun bingViewModel() {
        mDatabind.vm = mViewModel
    }

    private val adapterPrice: BaseAdapter = object : BaseAdapter() {
        override fun onCreateDefaultViewHolder(
            parent: ViewGroup,
            viewType: Int
        ): VipPriceHolder {
            return VipPriceHolder(parent)
        }
    }

    override fun createAdapter(): VipAdapter {
        return VipAdapter()
    }

    override fun initView() {
        super.initView()
        setMyTitle("开通VIP")
        recyclerviewPrice = headView.findViewById(R.id.recyclerview_price)
        recyclerviewPrice?.layoutManager = RecycleViewUtil.getLinear(this)
        adapterPrice.bindToRecyclerView(recyclerviewPrice)
    }

    override fun initListener() {
        super.initListener()
        //监听购买
        observe(payLiveSharedFlow) {
            it?.let {
                if (IThirdType.IBuyType.vip != it.buyType) {
                    return@observe
                }
                if (IThirdType.IPayStatus.FAIL == it.status) {
                    ToastUtils.showShort("支付失败！")
                    return@observe
                }
                if (IThirdType.IPayStatus.CANCEL == it.status) {
                    ToastUtils.showShort("支付取消！")
                    return@observe
                }
                try {
                    mViewModel.buyVip(it.extra[0] as VipConfigBean)
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
        }
        observe(mViewModel.vipConfigSucFlow) {
            getVipConfigSuc(it)
        }
        observe(mViewModel.priceFlow) {
            onPay(it.price, it.payBean, it.vipContentBean)
        }
        recyclerview?.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                if (recyclerview?.canScrollVertically(1) == false) {
                    mDatabind.tvTips.visibility = View.GONE
                    mDatabind.vBgFading.visibility = View.GONE
                }
            }
        })
        adapterPrice.setOnItemClickListener(BaseNewAdapter.OnItemClickListener<VipPriceHolder, VipConfigBean> { holder, bean ->
            val oldObject = adapterPrice.data[selectPosition]
            if (oldObject is VipConfigBean) {
                oldObject.isSelect = false
            }
            adapterPrice.notifyItemChangedAndHead(selectPosition)
            bean.isSelect = true
            holder.select()
            selectPosition = holder.curPosition
        })
    }

    override fun initData() {
        super.initData()
        reload()
    }

    override fun onListLoadPage(isPull: Boolean, isRefresh: Boolean, page_no: Int, page_size: Int) {
        mViewModel.getVipConfig()
    }

    override fun setListData(bean: ListViewBean) {
        if (bean.isRefresh && CollectionUtil.isEmptyOrNull(bean.list)) {
            if (adapter.headerLayoutCount > 0) {
                adapter.removeHeaderView(headView)
            }
        } else if (bean.isRefresh) {
            mDatabind.layoutBottom.visibility = View.VISIBLE
            if (adapter.headerLayoutCount == 0) {
                adapter.setHeaderView(headView)
            }
        }
        super.setListData(bean)
        recyclerview?.post {
            if (!recyclerview!!.canScrollVertically(1)) {
                mDatabind.tvTips.visibility = View.GONE
                mDatabind.vBgFading.visibility = View.GONE
            } else {
                mDatabind.tvTips.visibility = View.VISIBLE
                mDatabind.vBgFading.visibility = View.VISIBLE
            }
        }
    }

    private fun getVipConfigSuc(list: List<VipConfigBean>?) {
        adapterPrice.setNewData(list)
        if (list.isNullOrEmpty()) {
            showSuccess()
            showEmpty()
        } else {
            mViewModel.getData()
        }
    }

    fun onPay(price: BigDecimal, payBean: com.module.app.bean.PayBean, vipContentBean: VipConfigBean) {
        if (payBean == null || vipContentBean == null) {
            return
        }
        val title = vipContentBean.title
        PayPop.show(getContext(), IThirdType.IBuyType.vip, payBean, price, title, vipContentBean)
    }

    fun onClickSubmit(view: View) {
        if (!com.module.app.utils.UserUtils.isLogin(true)) {
            return
        }
        if (com.module.app.utils.UserUtils.isVip()) {
            ToastUtils.showShort("您当前已经是vip了")
            return
        }
        var vipConfigBean: VipConfigBean? = null
        val mObject = adapterPrice.data[selectPosition]
        if (mObject is VipConfigBean) {
            vipConfigBean = mObject
        }
        if (vipConfigBean != null) {
            if (vipConfigBean.vipPrice.toDouble() <= 0) {
                ToastUtils.showShort("最低支付0.01元")
                mViewModel.getPay(BigDecimal("0.01"), vipConfigBean)
            } else {
                mViewModel.getPay(vipConfigBean.vipPrice, vipConfigBean)
            }
        }
    }
}